package huawei;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 华为第二道编程题
 *
 * @Author Jun jie Yi
 * 2022/4/6 21:12
 */
public class StartService {
    public static void main(String[] args) {
        Queue<Integer> queue = new ArrayDeque<>();//队列用于暂存数据
        List<Integer> rely_sev = new ArrayList<>();//用于存储依赖的服务
        Scanner sc = new Scanner(System.in);
        //N 服务的总个数
        int N = sc.nextInt();
        //指定启动服务的编号
        int M = sc.nextInt();//2
        queue.offer(M);//入队
        sc.nextLine();//把空格读掉
        List<String> rely = new ArrayList<>();//0,1,2,3
        for (int i = 0; i < N; i++) {
            String s = sc.nextLine();
            if ("0".equals(s)) {
                rely.add("-1");//直接启动没有条件
            } else {
                rely.add(s.substring(2));
            }
        }
        //这里需要一个启动备忘录
        int[] logs = new int[N];
        //如果队列不为空
        while (!queue.isEmpty()) {
            Integer sev = queue.poll();//这里可能需要判断其是否有环
            rely_sev.add(sev);//加入存储集合中
            String s = rely.get(sev);
            int[] ints = transForInt(s);

            int flag = 0;
            for (int i = 0; i < ints.length; i++) {
                //有未启动
                if (logs[i] == 0) {
                    flag = 1;
                }
            }
            if ("-1".equals(s) || flag == 0) {//可以直接启动，因为没有启动条件
                logs[sev] = 1;
            }
            for (int i = 0; i < ints.length; i++) {
                if (logs[i] == 0) {//如果服务没有被启动
                    queue.offer(ints[i]);
                }
            }
        }
        List<Integer> temp;
        //如果后面的元素在前面出现就是错了
        for (int i = 1; i < rely_sev.size(); i++) {
            temp = rely_sev.subList(0, i);
            if (temp.contains(rely_sev.get(i))) {
                System.out.println(-1);
                return;
            }
        }
        rely_sev.remove(0);
        Collections.sort(rely_sev);
        for (int i = 0; i < rely_sev.size(); i++) {
            System.out.print(rely_sev.get(i));
            if (i != rely_sev.size() - 1) {
                System.out.print(",");
            }
        }
    }

    //字符转 int 数组
    static int[] transForInt(String param) {
        String[] r = param.split(",");
        return Arrays.stream(r).mapToInt(v -> Integer.parseInt(v)).toArray();
    }
}
